// 150811 gzf 只有非钥匙端才能感应加速度
//$(function(){
//	if (window.DeviceMotionEvent) {
//		window.addEventListener('devicemotion',deviceMotionHandler, false);
//	}
//});

function startSensor() {
	if (window.DeviceMotionEvent) {
		window.addEventListener('devicemotion', deviceMotionHandler, false);
	} else {
		alert("您的浏览器不能感应加速度，请更换浏览器！");
	}
}

function deviceMotionHandler(eventData) {
	var acceleration =eventData.accelerationIncludingGravity;
	//var acceleration = eventData.acceleration;

	var cur_time = new Date().getTime();
	var THRESHOLD = 12;
	var last_update=0;
	var x,y,z,last_x=0,last_y=0,last_z=0;
	
	if(cur_time - last_update > 500){
		var diff_time = cur_time - last_update;
		last_update = cur_time;
		x = acceleration.x;
		y = acceleration.y;
		z = acceleration.z;
		//var speed = Math.abs(1)/diff_time*10000;
		var speed = Math.abs(x+y+z-last_x-last_y-last_z);
		//alert(speed);
		if(speed > THRESHOLD){
			//alert("shaked!");
			eventsender(EVENT_AV);
		}
		last_x = x;
		last_y = y;
		last_z = z;
	}
}

